using System.Collections.Generic;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;
using UnityEngine.EventSystems;

public class UIAnimation : MonoBehaviour
{
	public EventTrigger.TriggerEvent animation;

	public ExecuteOn executeOn;

	public float defaultDuration = 1f;

	public EventTrigger.TriggerEvent callback;

	private UIRect _uirect;

	private Vector3 initialScale = Vector3.one;

	private float initialAlpha = 1f;

	public UpdateType _hotweenUpdateType;

	private Tweener waitTweener;

	[HideInInspector]
	public float waitCallBackProgress;

	private Tweener scaleTweener;

	private Tweener alphaTweener;

	private Tweener positionTweener;

	private UIRect uirect
	{
		get
		{
			if (_uirect == null)
			{
				_uirect = GetComponent<UIRect>();
				initialAlpha = _uirect.alpha;
			}
			return _uirect;
		}
	}

	private void Awake()
	{
		initialScale = base.transform.localScale;
	}

	private void OnEnable()
	{
		base.transform.localScale = initialScale;
		if (uirect != null)
		{
			uirect.alpha = initialAlpha;
		}
		if (executeOn == ExecuteOn.OnEnable)
		{
			StartAnimation();
		}
	}

	private void Start()
	{
		if (executeOn == ExecuteOn.Start)
		{
			StartAnimation();
		}
	}

	private void StartAnimation()
	{
		if (animation != null)
		{
			animation.Invoke(null);
		}
	}

	private void AnimationFinished()
	{
		if (callback != null)
		{
			callback.Invoke(null);
		}
	}

	public void WaitAndCallBack(float time)
	{
		waitCallBackProgress = 0f;
		waitTweener = HOTween.To(this, time, new TweenParms().Prop("waitCallBackProgress", 100).UpdateType(_hotweenUpdateType).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			AnimationFinished();
		}));
	}

	public void AnimPopOnScale(string parametrs)
	{
		List<float> parametrList = ParsParametrs(parametrs);
		float fromParametr;
		float toParametr;
		float duration;
		bool callBack;
		GetParametrs(parametrList, out fromParametr, out toParametr, out duration, out callBack);
		AnimPopOnScale(fromParametr, toParametr, duration, callBack);
	}

	public void AnimPopOnScale(float fromParametr, float toParametr, float duration, bool callBack)
	{
		if (scaleTweener != null)
		{
			HOTween.Kill(scaleTweener);
			scaleTweener = null;
		}
		base.transform.localScale = initialScale * fromParametr;
		scaleTweener = HOTween.To(base.transform, duration, new TweenParms().Prop("localScale", initialScale * toParametr).Ease(EaseType.EaseInExpo).UpdateType(_hotweenUpdateType)
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				scaleTweener = HOTween.To(base.transform, duration, new TweenParms().Prop("localScale", initialScale).Ease(EaseType.EaseInOutExpo).UpdateType(_hotweenUpdateType)
					.OnComplete((TweenDelegate.TweenCallback)delegate
					{
						if (callBack)
						{
							AnimationFinished();
						}
					}));
			}));
	}

	public void AnimScale(string parametrs)
	{
		List<float> parametrList = ParsParametrs(parametrs);
		float fromParametr;
		float toParametr;
		float duration;
		bool callBack;
		GetParametrs(parametrList, out fromParametr, out toParametr, out duration, out callBack);
		AnimScale(fromParametr, toParametr, duration, callBack);
	}

	public void AnimScale(float fromParametr, float toParametr, float duration, bool callBack)
	{
		if (scaleTweener != null)
		{
			HOTween.Kill(scaleTweener);
			scaleTweener = null;
		}
		base.transform.localScale = initialScale * fromParametr;
		scaleTweener = HOTween.To(base.transform, duration, new TweenParms().Prop("localScale", initialScale * toParametr).Ease(EaseType.EaseInExpo).UpdateType(_hotweenUpdateType)
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				if (callBack)
				{
					AnimationFinished();
				}
			}));
	}

	public void AnimPopOnAlpha(string parametrs)
	{
		if (uirect == null)
		{
			Debug.LogError("Can't animate alpha of non UIRect object!");
			return;
		}
		List<float> parametrList = ParsParametrs(parametrs);
		float fromParametr;
		float toParametr;
		float duration;
		bool callBack;
		GetParametrs(parametrList, out fromParametr, out toParametr, out duration, out callBack);
		AnimPopOnAlpha(fromParametr, toParametr, duration, callBack);
	}

	public void AnimPopOnAlpha(float fromParametr, float toParametr, float duration, bool callBack)
	{
		if (alphaTweener != null)
		{
			HOTween.Kill(alphaTweener);
			alphaTweener = null;
		}
		uirect.alpha = fromParametr;
		alphaTweener = HOTween.To(uirect, duration, new TweenParms().Prop("alpha", toParametr).Ease(EaseType.EaseInExpo).UpdateType(_hotweenUpdateType)
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				scaleTweener = HOTween.To(uirect, duration, new TweenParms().Prop("alpha", initialAlpha).Ease(EaseType.EaseInOutExpo).UpdateType(_hotweenUpdateType)
					.OnComplete((TweenDelegate.TweenCallback)delegate
					{
						if (callBack)
						{
							AnimationFinished();
						}
					}));
			}));
	}

	public void AnimAlpha(string parametrs)
	{
		if (uirect == null)
		{
			Debug.LogError("Can't animate alpha of non UIRect object!");
			return;
		}
		List<float> parametrList = ParsParametrs(parametrs);
		float fromParametr;
		float toParametr;
		float duration;
		bool callBack;
		GetParametrs(parametrList, out fromParametr, out toParametr, out duration, out callBack);
		AnimAlpha(fromParametr, toParametr, duration, callBack);
	}

	public void AnimAlpha(float fromParametr, float toParametr, float duration, bool callBack)
	{
		if (alphaTweener != null)
		{
			HOTween.Kill(alphaTweener);
			alphaTweener = null;
		}
		uirect.alpha = fromParametr;
		alphaTweener = HOTween.To(uirect, duration, new TweenParms().Prop("alpha", toParametr).UpdateType(UpdateType.Update).Ease(EaseType.EaseInExpo)
			.UpdateType(_hotweenUpdateType)
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				if (callBack)
				{
					AnimationFinished();
				}
			}));
	}

	public void AnimPosition(string parametrs)
	{
		List<float> list = ParsParametrs(parametrs);
		if (list.Count >= 6)
		{
			Vector3 fromPosition = new Vector3(list[0], list[1], list[2]);
			Vector3 toPosition = new Vector3(list[3], list[4], list[5]);
			float duration = defaultDuration;
			bool callBack = false;
			if (list.Count > 6)
			{
				duration = list[6];
			}
			if (list.Count > 7 && list[7] > 0f)
			{
				callBack = true;
			}
			AnimPosition(fromPosition, toPosition, duration, callBack);
		}
	}

	public void AnimPosition(Vector3 fromPosition, Vector3 toPosition, float duration, bool callBack)
	{
		if (positionTweener != null)
		{
			HOTween.Kill(positionTweener);
			positionTweener = null;
		}
		base.transform.localPosition = fromPosition;
		positionTweener = HOTween.To(base.transform, duration, new TweenParms().Prop("localPosition", toPosition).Ease(EaseType.EaseInExpo).UpdateType(_hotweenUpdateType)
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				if (callBack)
				{
					AnimationFinished();
				}
			}));
	}

	private List<float> ParsParametrs(string parametrsString)
	{
		List<float> list = new List<float>(0);
		string[] array = parametrsString.Split(" ,;".ToCharArray());
		string[] array2 = array;
		foreach (string s in array2)
		{
			float result = 0f;
			if (float.TryParse(s, out result))
			{
				list.Add(result);
			}
		}
		return list;
	}

	private void GetParametrs(List<float> parametrList, out float fromParametr, out float toParametr, out float duration, out bool callBack)
	{
		fromParametr = 0f;
		toParametr = 0f;
		duration = defaultDuration;
		callBack = false;
		if (parametrList.Count > 0)
		{
			fromParametr = parametrList[0];
		}
		if (parametrList.Count > 1)
		{
			toParametr = parametrList[1];
		}
		if (parametrList.Count > 2)
		{
			duration = parametrList[2];
		}
		if (parametrList.Count > 3 && parametrList[3] > 0f)
		{
			callBack = true;
		}
	}
}
